package com.hrw.musicplayer.db;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class DataBaseProvider extends ContentProvider {
	public static final String AUTHORITY = "com.hrw.musicplayer.db.DataBaseProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + FileTable.TABLE_NAME);
	private DataBaseHelper dataBaseHelper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
		long count = 0;
		try {
			count = db.insert(FileTable.TABLE_NAME, null, values);
		} catch (Exception ex) {
			Log.e("error", "insert:" + ex.getMessage());
		}
		if (count > 0)
			return uri;
		else
			return null;
	}

	@Override
	public boolean onCreate() {
		dataBaseHelper = new DataBaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
		// 依次参数为：表明，查询字段，where语句,占位符替换，group by(分组)，having(分组条件),order by(排序)
		Cursor cur = db.query(FileTable.TABLE_NAME, projection, selection,
				selectionArgs, null, null, sortOrder);
		return cur;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
